/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.client.ui.card.impl;

import java.util.Collection;

import org.jb.coinche.client.business.model.Card;
import org.jb.coinche.client.ui.card.CardList;

import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.AbsoluteData;
import com.extjs.gxt.ui.client.widget.layout.AbsoluteLayout;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Image;

/**
 * 
 * @author jeremie
 *
 */
public class CardListImpl extends LayoutContainer implements CardList {


	
	/**
	 * 
	 */
	public CardListImpl() {
		setLayout( new AbsoluteLayout() );
		setHeight( 155 );
		clickable = false;
	}
	
	/* (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.impl.CardList#setCardListener(org.jb.coinche.client.gui.card.impl.CardListImpl.CardListener)
	 */
	public void setCardListener( CardListener cardListener ) {
		this.cardListener = cardListener;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.CardList#setCardList(java.util.Collection)
	 */
	public void setCardList( Collection<Card> cards ) {
		setCardList( cards, null );
		
	}
	/* (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.impl.CardList#setCardList(java.util.Collection)
	 */
	public void setCardList( Collection<Card> cards, Collection<Card> playables ) {
	
		int offset = 0;
		removeAll();
		
		for( final Card card : cards ) {
			
			Boolean b = playables == null ? true : playables.contains(card);
			
			Image image = new Image( CardPanelImpl.getImage(card) );
			if (b) {
				image.addClickHandler( new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						notifyCardClick( card );
					}
				} );
			}

			add( image, new AbsoluteData(offset, 0) );
			
			if (!b) {
				Html html = new Html( "<div style=\"opacity: 0.2; background-color: orange; width: 104; height:154\"></div>" );
				html.setStyleAttribute("cursor", "auto");
				add( html, new AbsoluteData(offset, 0 ) );
				
			}
			offset += 40;
		}
		
		layout();
	}
	
	/* (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.impl.CardList#setClickable(boolean)
	 */
	public void setClickable( boolean clickable ) {
		this.clickable = clickable;
		
		setStyleAttribute("cursor", !clickable ? "auto" : "pointer" );
	}
	
	/**
	 * 
	 * @param card
	 */
	private void notifyCardClick( Card card ) {
		if (!clickable)
			return;
		
		if (cardListener != null)
			cardListener.cardSelected(card);
	}
	
	private boolean clickable;
	private CardListener cardListener;

}
